<template>
    <view class="user-popup" :class="uiStyle == 1 ? '' : 'uiStyle'" >
        <!-- <van-dialog id="van-dialog" confirm-button-color="#00BCAB" /> -->
        <view class="my-top">
            <view class="top-bg"><image src="../../static/assets/img/mybg.png"></image></view>
            <view class="user-title" v-if="isLogin">
                <view class="uilogo" style="padding: 0 35rpx; width: 104rpx; height: 104rpx">
                    <image :src="faceImageUrl?faceImageUrl:'../../static/assets/img/person.png'"  style="width: 104rpx; height: 104rpx; border-radius: 50%" />
                    <view class="setting-icon" @tap="handleusermessage">
                        <!-- <image src="/assets/img/user/sz.png"></image> -->
                        <!-- <image src="/assets/img/user/edit.png"></image> -->
                        <text class="edit-box">编辑资料</text>
                    </view>
                </view>
                <view class="user-title-right">
                    <view style="font-size: 40rpx; color: #ffffff" v-if="username">{{ username }}</view>
                    <view style="font-size: 26rpx; color: #ffffff; opacity: 0.8">{{ phoneNumber }}</view>
                    <!-- <view style="font-size: 26rpx">{{plateNos}}</view> -->
                </view>
                <view :class="isqd ? 'setting-qd-comm setting-complete-complete-qd' : 'setting-qd-comm setting-qd'" v-if="uiStyle == 1">
                    <image class="qd-icon" src="../../static/assets/img/qd.png"></image>
                    <view v-if="isqd" class="qd-box">
                        <view class="tody-qd">已签到</view>
                        <view class="tody-jf">+10积分</view>
                    </view>
                    <view v-else class="qd-box" @tap="handSign">
                        <view class="tody-qd">今日签到</view>
                        <view class="tody-jf">+10积分</view>
                    </view>
                </view>
            </view>
            <view class="user-title" v-else @tap="gotoLogin">
                <view style="padding: 0 35rpx; width: 104rpx; height: 104rpx">
                    <image src="../../static/assets/img/user/unlogin.png" style="width: 100%; height: 100%; border-radius: 50%" />
                </view>
                <view class="user-title-right">
                    <view style="font-size: 40rpx; color: #ffffff">请点击登录</view>
                </view>
            </view>
            <view class="top-col-card">
                <view class="col-item" @tap="gotoLicenseManage">
                    <view class="col-img-container"><image src="../../static/assets/img/myLicense.png" style="width: 48rpx; height: 45rpx; padding-top: 6rpx"></image></view>
                    <view class="col-item-text">车牌</view>
                </view>
                <view class="col-item" @tap="handleOrder">
                    <view class="col-img-container"><image src="../../static/assets/img/myOrder.png" style="width: 38rpx; height: 45rpx"></image></view>
                    <view class="col-item-text">订单</view>
                </view>
                <!-- <view class="col-item" bindtap="goBill">
                <view class="col-img-container"><image src="/assets/img/ticket.png"></image></view>
                <view class="col-item-text">开发票</view>
            </view> -->

                <view class="col-item" @tap="gotoCoupon">
                    <view class="col-img-container"><image src="../../static/assets/img/jifenduihuanicon.png" style="width: 45rpx; height: 47rpx"></image></view>
                    <view class="col-item-text">积分</view>
                </view>
                <!-- <view class="col-item" bindtap="handleCallKf">
                <view class="col-img-container"><image src="/assets/img/kefu.png"></image></view>
                <view class="col-item-text">客服</view>
            </view> -->
                <view class="col-item" @tap="Gowallet">
                    <view class="col-img-container"><image src="../../static/assets/img/myWallet.png"></image></view>
                    <view class="col-item-text">钱包</view>
                </view>
            </view>
        </view>
        <view style="">
            <view class="user-body">
                <view class="part1">
                    <!-- <view class="body-item" style="margin-top: 0" bindtap="toNormalQuestion">
                <image src="/assets/img/user/usehelp.png" style="margin-right: 0;width:40rpx;height:40rpx" />
                <view class="body-item-text">使用帮助</view>
                <view class="arrow-img"><image src="/assets/img/arrow.png"></image></view> -->
                    <view class="body-item" style="margin-top: 0" @tap="personal">
                        <image src="../../static/assets/img/user/usehelp.png" style="margin-right: 0; width: 40rpx; height: 40rpx" />
                        <view class="body-item-text">用户服务协议</view>
                        <view class="arrow-img"><image src="../../static/assets/img/arrow.png"></image></view>
                        <!-- <van-icon name="arrow" size="30rpx" style="margin-right: 0;margin-left: auto" /> -->
                    </view>
                    <view class="body-line"></view>
                    <view class="body-item" style="margin-top: 0" @tap="toPlatformRules">
                        <image src="../../static/assets/img/user/ptgz.png" style="margin-right: 0; width: 40rpx; height: 40rpx" />
                        <view class="body-item-text">隐私政策</view>
                        <view class="arrow-img"><image src="../../static/assets/img/arrow.png"></image></view>
                    </view>
                    <view class="body-item" style="margin-top: 0" @tap="goBill">
                        <image src="../../static/assets/img/user/fpkj.png" style="margin-right: 0; width: 40rpx; height: 40rpx" />
                        <view class="body-item-text">发票开具</view>
                        <view class="arrow-img"><image src="../../static/assets/img/arrow.png"></image></view>
                    </view>
                </view>

                <view class="part2">
                    <!-- <view class="body-line"></view> -->
                    <!-- <view class="bg-line"></view> -->

                    <!-- <view class="bg-line"></view> -->

                    <view class="body-item" style="margin-top: 0" @tap="joinUS">
                        <image src="../../static/assets/img/user/joinicon.png" style="margin-right: 0; width: 40rpx; height: 40rpx" />
                        <view class="body-item-text">加入邻里停</view>
                        <view class="arrow-img"><image src="../../static/assets/img/arrow.png"></image></view>
                    </view>
                    <view class="body-line"></view>
                    <!-- <view class="body-item" style="margin-top: 0">
                    <image src="/assets/img/user/shareicon.png" style="margin-right: 0;width:40rpx;height:40rpx" />
                    <view class="body-item-text">
                    <button open-type="share">分享小程序</button>
                    </view>
                    <view class="arrow-img"><image src="/assets/img/arrow.png"></image></view>
                </view> -->
                    <view class="body-item" style="margin-top: 0" @tap="feedback">
                        <image src="../../static/assets/img/feedback.png" style="margin-right: 0; width: 40rpx; height: 40rpx" />
                        <view class="body-item-text">意见反馈</view>
                        <view class="arrow-img"><image src="../../static/assets/img/arrow.png"></image></view>
                    </view>
                    <view class="body-line"></view>
                    <view class="body-item" style="margin-top: 0" @tap="handleCallKf">
                        <image src="../../static/assets/img/user/lxkf.png" style="margin-right: 0; width: 40rpx; height: 40rpx" />
                        <view class="body-item-text">联系客服</view>
                        <view class="arrow-img"><image src="../../static/assets/img/arrow.png"></image></view>
                    </view>
                </view>
            </view>
        </view>
        <view v-if="isLogin" class="out-btn" @tap="Loginout" style="display: none">退出登录</view>
		<view style="height:120rpx">
			
		</view>
    </view>
</template>

<script>
// import vanIcon from '@vant/weapp/icon/index';
import util from './../../utils/util.js';
import api from './../../utils/api.js';
// import Dialog from './../../miniprogram_npm/@vant/weapp/dialog/dialog.js';
export default {
    components: {
        // vanIcon
    },
    data() {
        return {
            username: '',
            phoneNumber: '',
            plateNos: '',
            faceImageUrl: '',
            banner: [],
            list: [],
            allMoney: ' ',
            withdrawalMoney: ' ',
            balance: ' ',
            isLogin: false,
			uiStyle:1,
            isqd: false //是否签到
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		this.uiStyle = uni.getStorageSync('uiStyle')
        const unionId = uni.getStorageSync('unionId');
        this.getPersonInfoByIndentityId(unionId);
    },
    // 分享小程序
    // onShareAppMessage: function (options) {
    //   var unionId = wx.getStorageSync('unionId') || '';
    //   var activityCode = wx.getStorageSync('activityCode') || '';
    //   return {
    //     title: '共享停车 方便你我',
    //     path: '/pages/index/index?shareId=' + unionId+"&activityCode="+activityCode, //这里拼接需要携带的参数
    //     imageUrl: 'https://shareparking.wasu.cn/share-parking/static/shareImg.jpg',
    //     success: function (res) {
    //       if(wx.getStorageSync('AUTHORIZATION')){
    //         // 此处调用接口发送分享人信息
    //       }
    //     },
    //     fail:function(err){
    //       console.log('转发失败'+err)
    //     }
    //   }
    // },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.isWaitingToRead();

        if (uni.getStorageSync('AUTHORIZATION')) {
            this.setData({
                isLogin: true
            });
            const unionId = uni.getStorageSync('unionId');
            this.getPersonInfoByIndentityId(unionId); //暂时不要从接口取头像 会导致每次都会加载闪一下

            this.setData({
                faceImageUrl: uni.getStorageSync('faceImageUrl')
            });
            this.todaySignStatus(unionId); // this.getmyInCome(unionId);
        } else {
            this.setData({
                isLogin: false
            });
        } // 存储当前页面-登陆后跳转至登录前页面

        const page = getCurrentPages();
        uni.setStorageSync('lastPagePath', '/' + page[0].route);
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    methods: {
        handSign() {
            //手动签到
            const unionId = uni.getStorageSync('unionId') || '';
            util.request(api.todaySign + '?unionId=' + unionId, 'get').then((res) => {
                if (res.code == 0) {
                    uni.showToast({
                        title: res.data,
                        icon: 'none'
                    });
                    this.setData({
                        isqd: true
                    });
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            });
        },

        todaySignStatus(unionId) {
            //获取签到状态
            util.request(api.todaySignStatus + '?unionId=' + unionId, 'get').then((res) => {
                if (res.code == 0) {
                    this.setData({
                        isqd: res.data
                    });
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            });
        },

        //联系客服
        handleCallKf() {
            const unionId = uni.getStorageSync('unionId') || '';
            const param = {
                type: 3,
                carOrderLogId: ''
            };
            util.request(api.call + '?carOrderLogId=' + param.carOrderLogId + '&userUnionId=' + unionId + '&type=' + param.type, 'get').then((res) => {
                if (res.code == 0) {
                    if(uni.getStorageSync('weixin') == 3 || uni.getStorageSync('weixin') == 4){
                    	ZWJSBridge.phoneCall({
                    	  "corpId": res.data
                    	}).then(res => {
                    	  console.log(res)
                    	}).catch(err => {
                    	  console.log(err) 
                    	})
                    }else{
                    	uni.makePhoneCall({
                    		phoneNumber: res.data
                    	}).catch((e) => {
                    		console.log(e);
                    	});
                    }
                } else {
                }
            });
        },

        //钱包
        Gowallet() {
            if (uni.getStorageSync('AUTHORIZATION')) {
                uni.navigateTo({
                    url: '../wallet/index'
                });
            } else {
                this.gotoLogin();
            }
        },

        handleSuggest() {
            if (uni.getStorageSync('AUTHORIZATION')) {
                uni.navigateTo({
                    url: '../suggestion/index'
                });
            } else {
                this.gotoLogin();
            }
        },

        //我的订单
        handleOrder() {
            if (uni.getStorageSync('AUTHORIZATION')) {
                uni.navigateTo({
                    url: '../order/index'
                });
            } else {
                this.gotoLogin();
            }
        },

        //修改个人信息
        handleusermessage() {
            // wx.navigateTo({
            //   url: "../changeUser/index"
            // });
            uni.navigateTo({
                url: '../newChangeUser/newChangeUser'
            });
        },

        // 登出
        Loginout() {
            Dialog.confirm({
                context: this,
                message: '确定退出账号？'
            })
                .then(() => {
                    // on confirm
                    // wx.setStorage({ key: "phoneNumber", data: this.data.value }); //手机号
                    // wx.setStorage({ key: "sms", data: this.data.sms }); //手机号
                    uni.removeStorageSync('name');
                    uni.removeStorageSync('phoneNumber');
                    uni.removeStorageSync('plateNos');
                    uni.removeStorageSync('faceImageUrl');
                    uni.removeStorageSync('status');
                    uni.removeStorageSync('sms');
                    uni.removeStorageSync('AUTHORIZATION');
                    uni.removeStorageSync('identityId');
                    uni.removeStorageSync('unionId');
                    uni.removeStorageSync('shareId');
                    uni.removeStorageSync('activityCode');
                    uni.redirectTo({
                        url: '../login/index'
                    });
                })
                .catch(() => {
                    // on cancel
                });
        },

        //我的钱包
        getmyInCome(unionId) {
            util.request(api.getMyInCome + '?unionId=' + unionId, 'get').then((res) => {
                if (res.code == 0) {
                    const allMoney = res.data.allMoney ? res.data.allMoney / 100 : 0;
                    const withdrawalMoney = res.data.withdrawalMoney ? res.data.withdrawalMoney / 100 : 0;
                    const balance = res.data.allMoney ? +res.data.allMoney / 100 : 0;
                    this.setData({
                        allMoney: allMoney,
                        withdrawalMoney: withdrawalMoney,
                        balance: balance
                    });
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            });
        },

        //获取个人资料
        getPersonInfoByIndentityId(value) {
            util.request(api.PersonInfoByIndentityId + '?personUnionId=' + value, 'get').then((res) => {
                if (res.code == 0) {
                    var pat = /(\d{3})\d*(\d{4})/;
                    this.setData({
                        username: res.data.name ? res.data.name : '',
                        phoneNumber: res.data.phoneNumber ? res.data.phoneNumber.replace(pat, '$1****$2') : '',
                        plateNos: res.data.plateNos ? res.data.plateNos.map((e) => e.split(',')) : ''
                    });
                } //头像转化

                if (res.data && res.data.headImageUrl) {
                    util.request(api.getEncryPath + '?path=' + res.data.headImageUrl, '', 'POST').then((res) => {
                        this.setData({
                            faceImageUrl: res
                        });
                        uni.setStorageSync('faceImageUrl', res);
                    });
                } else {
                    this.setData({
                        faceImageUrl: ''
                    });
                } // 上传完成需要更新 fileList
            });
        },

        // 平台规则
        toPlatformRules() {
            uni.navigateTo({
                url: '../platformRules/platformRules'
            });
        },

        //常见问题
        toNormalQuestion() {
            uni.navigateTo({
                url: '../nomalQuestion/nomalQuestion'
            });
        },

        //用户隐私说明
        personal() {
            uni.navigateTo({
                url: '../personal/index'
            });
        },

        //消息中心
        messageCenter() {
            uni.showToast({
                title: '功能开发中，敬请期待',
                icon: 'none'
            });
        },

        // 跳转登录页
        gotoLogin() {
			if(uni.getStorageSync('weixin') == 2){
				jWeixin.miniProgram.reLaunch({
					url: "/pages/login/login"
				})
			}else{
				uni.navigateTo({
					url: '../login/index'
				});				
			}

        },

        // 车牌管理
        gotoLicenseManage() {
            if (uni.getStorageSync('AUTHORIZATION')) {
                uni.navigateTo({
                    url: '../licenseManage/licenseManage'
                });
            } else {
                this.gotoLogin();
            }
        },

        // 优惠券
        gotoCoupon() {
            if (uni.getStorageSync('AUTHORIZATION')) {
                uni.navigateTo({
                    url: '../myPoints/myPoints'
                });
            } else {
                this.gotoLogin();
            }
        },

        // 发票开具
        goBill() {
            uni.navigateTo({
                url: '../billIndex/index'
            });
        },

        // 加入邻里停
        joinUS() {
            uni.navigateTo({
                url: '../joinUS/joinUS'
            });
        },

        // 建议反馈
        feedback() {
            // wx.navigateTo({
            //   url: '../suggestion/index',
            // })
            uni.showModal({
                title: '意见反馈',
                content: '',
                editable: true,
                showCancel: true,
                confirmText: '提交',
                confirmColor: '#FFA000',

                success(res) {
                    console.log(res.content);

                    if (!res.content) {
                        return;
                    }

                    const param = {
                        identityId: uni.getStorageSync('identityId'),
                        nickname: uni.getStorageSync('name'),
                        phone: uni.getStorageSync('phoneNumber'),
                        suggest: res.content
                    };
                    util.request(api.userSuggest, param, 'POST').then((res) => {
                        if (res.code == 0) {
                            uni.showToast({
                                title: '提交成功！',
                                icon: 'success'
                            });
                        } else {
                            uni.showToast({
                                title: res.msg,
                                icon: 'success'
                            });
                        }
                    });
                }
            });
        },

        isWaitingToRead() {
            //新消息留言
            const unionId = uni.getStorageSync('unionId');

            if (!unionId) {
                return;
            }

            util.request(
                api.isWaitingToRead,
                {
                    unionId: unionId
                },
                'POST',
                'application/x-www-form-urlencoded'
            ).then((res) => {
                console.log(res);

                if (res.data) {
                    uni.showTabBarRedDot({
                        index: 1
                    });
                } else {
                    uni.hideTabBarRedDot({
                        index: 1
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './my.css';
</style>
